package arina.sh.ethernetcontrol;

import java.net.InetAddress;
import java.net.UnknownHostException;

import android.annotation.SuppressLint;
import android.os.Parcel;
import android.os.Parcelable;

@SuppressLint("ParcelCreator")
public class System_Device implements Parcelable {
	String mLocalIP;
	int mPort;
	String mDDNS;
	String mExternalIP;
	String mUsername;
	String mPassword;
	//public List<Relay> arrRelay;
	public String getExternalIP() {
		return this.mExternalIP;
	}

	public void setExternalIP(String dDNS) {
		try {
			this.mExternalIP = (InetAddress.getByName(dDNS)).getHostAddress();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			this.mExternalIP="";
		}
	}
	int mSubnetID;
	
	
	
	
	
	
	public System_Device(String localIP, int port, String dDNS, String username, String password,
			int subnetID)  {
		super();
		try {
			this.mLocalIP =localIP;
			this.mExternalIP=(InetAddress.getByName(dDNS)).getHostAddress();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		this.mPort = port;
		this.mDDNS = dDNS;
		this.mUsername = username;
		this.mPassword = password;
		this.mSubnetID = subnetID;
		this.mLocalIP =localIP;
	}
	
	public void sendCommand(COMMAND_TYPE cmd_type,int device_id,int device_channel,int brightness_level,NETWORK_STATUS mNetworkStatus)
	{
		String currIP=mLocalIP;
		if(mNetworkStatus==NETWORK_STATUS.EXTERNAL)
		{
			currIP=mExternalIP;
			
		}
		helperClass.makeDataToSend(cmd_type,mSubnetID,device_id,device_channel,brightness_level,mUsername,mPassword,currIP,mPort);
//		byte[]udpMsg= helperClass.makeDataToSend(cmd_type,mSubnetID,device_id,device_channel,brightness_level,mUsername,mPassword);
//		DatagramSocket ds=null;
//		try{
//		ds= new DatagramSocket();
//		
//		
//		DatagramPacket dp=new DatagramPacket(udpMsg,udpMsg.length,InetAddress.getByName(currIP),mPort);
//		ds.send(dp);
//		}
//		catch(SocketException e)
//		{
//			
//		} catch (UnknownHostException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}finally{
//			if(ds!=null)
//				ds.close();
//		}
		
	}
//	private byte[] makeDataToSend(COMMAND_TYPE cmd_type,int SUBNET_ID,int DEVICE_ID,int DEVICE_CHANNEL ,int BRIGHTNESS_VARIABLE ) {
//		// TODO Auto-generated method stub
//		byte code;
//		switch (cmd_type) {
//		case ON_OFF:
//			code=0x31;
//			break;
//		case REVERSE:
//			code=0x1c;
//			break;
//		case READ_STATUS:
//			code=0x33;
//			break;
//
//		default:
//			code=0x33;
//			break;
//		}
//		byte[] array_byte_username= mUsername.getBytes();
//		byte[] array_byte_password=mPassword.getBytes();
//		byte[] headMsg = new byte[]
//				{
//	        (byte) 83,
//	        (byte) 77,
//	        (byte) 65,
//	        (byte) 82,
//	        (byte) 84,
//	        (byte) 72,
//	        (byte) 79,
//	        (byte) 85,
//	        (byte) 83,
//	        (byte) 69};
//		
//	    byte[] middleMsg=new byte[]{    
//	        (byte)0x00,
//	        code,
//	        (byte)SUBNET_ID,
//	        (byte)DEVICE_ID
//	       
//	      };
//	    
//	    byte[] tailMsg=new byte[]{
//	    		 (byte)DEVICE_CHANNEL,
//	 	        (byte)BRIGHTNESS_VARIABLE,
//	 	        (byte)0x00,
//	 	        (byte)0x00
//	    };
//	    byte[] udpMsg=new byte[headMsg.length+array_byte_username.length+array_byte_password.length+middleMsg.length+tailMsg.length];
//		System.arraycopy(headMsg, 0, udpMsg, 0, headMsg.length);
//		System.arraycopy(array_byte_username, 0, udpMsg, headMsg.length,array_byte_username.length);
//		System.arraycopy(array_byte_password, 0, udpMsg, headMsg.length+array_byte_username.length, array_byte_password.length);
//		System.arraycopy(middleMsg, 0, udpMsg, headMsg.length+array_byte_username.length+array_byte_password.length, middleMsg.length);
//		
//		if(cmd_type!=COMMAND_TYPE.READ_STATUS)
//			System.arraycopy(tailMsg, 0, udpMsg, headMsg.length+array_byte_username.length+array_byte_password.length+middleMsg.length, tailMsg.length);
//	    return udpMsg;
//	}
	public int getPort() {
		return this.mPort;
	}
	public void setPort(int port) {
		this.mPort = port;
	}
	public String getDDNS() {
		return this.mDDNS;
	}
	public void setDDNS(String dDNS) {
		this.mDDNS = dDNS;
	}
	
	public String getLocalIP() {
		return this.mLocalIP;
	}
	public void setLocalIP(String localIP) {
		this.mLocalIP = localIP;
	}

	public String getUsername() {
		return this.mUsername;
	}
	public void setUsername(String username) {
		this.mUsername = username;
	}
	public String getPassword() {
		return this.mPassword;
	}
	public void setPassword(String password) {
		this.mPassword = password;
	}
	public int getSubnetID() {
		return this.mSubnetID;
	}
	public void setSubnetID(int subnetID) {
		this.mSubnetID = subnetID;
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}
	
	public System_Device(Parcel in)
	{
		try {
			
			this.mExternalIP=(InetAddress.getByName(in.readString())).getHostAddress();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		this.mPort = in.readInt();
		this.mDDNS = in.readString();
		this.mUsername = in.readString();
		this.mPassword = in.readString();
		this.mSubnetID = in.readInt();
		this.mLocalIP =in.readString();
		
	}
	@Override
	public void writeToParcel(Parcel dest, int flags) {
		// TODO Auto-generated method stub
		dest.writeString(mExternalIP);
		
		dest.writeInt(mPort);
		dest.writeString(mDDNS);
		
		dest.writeString(mUsername);
		dest.writeString(mPassword);
		dest.writeInt(mSubnetID);
		dest.writeString(mLocalIP);
	}
	
	public static final Parcelable.Creator<System_Device> CREATOR
    = new Parcelable.Creator<System_Device>() {
    	@Override
		public System_Device createFromParcel(Parcel in) {
    	    return new System_Device(in);
    	}

    	@Override
		public System_Device[] newArray(int size) {
    	    return new System_Device[size];
    	}

    };


}
